//
// Created by zzj on 2024/12/22.
//

#include "VideoPlatform.h"

#include <iostream>
#include <ranges>
#include <QDebug>

QList<VideoPlatform *> VideoPlatform::VideoRecords(const QString &tableName, const QString &dayOfWeekEnglish)
{
    const QString sql = "SELECT * FROM " + tableName + " where " + dayOfWeekEnglish + "!=-1 ORDER BY update_time ASC";
    auto tupleRecords = SqliteDao::QueryRecords(sql);

    QList<VideoPlatform *> resultList;
    resultList.resize(tupleRecords.size());
    std::ranges::transform(tupleRecords, resultList.begin(), [&](auto &record) {
        auto *platform = new VideoPlatform();
        platform->SetRecord(record);
        return platform;
    });
    return resultList;
}

[[nodiscard]] int VideoPlatform::GetSqlTotalCount(const QString &platformName)
{
    return SqliteDao::QueryCount(
        "SELECT \n"
        "    (SUM(CASE WHEN Mon != -1 THEN 1 ELSE 0 END) +\n"
        "     SUM(CASE WHEN Tue != -1 THEN 1 ELSE 0 END) +\n"
        "     SUM(CASE WHEN Wed != -1 THEN 1 ELSE 0 END) +\n"
        "     SUM(CASE WHEN Thu != -1 THEN 1 ELSE 0 END) +\n"
        "     SUM(CASE WHEN Fri != -1 THEN 1 ELSE 0 END) +\n"
        "     SUM(CASE WHEN Sat != -1 THEN 1 ELSE 0 END) +\n"
        "     SUM(CASE WHEN Sun != -1 THEN 1 ELSE 0 END))\n"
        "FROM " + platformName);
}

[[nodiscard]] int VideoPlatform::GetSqlDayCount(const QString &tableName, const QString &dayOfWeekEnglish)
{
    return SqliteDao::QueryCount(
        "SELECT COUNT(*) FROM " + tableName + " where " + dayOfWeekEnglish + "!=-1");
}

void VideoPlatform::AddAlreadyRead(const QString &tableName, const int id)
{
    const QString sql = "UPDATE " + tableName + " SET read_episode = read_episode + 1 WHERE id = " +
                        QString::number(id);
    QSqlQuery query;
    query.exec(sql);
}

[[nodiscard]] int VideoPlatform::GetId() const
{
    return m_id;
}

[[nodiscard]] QString VideoPlatform::GetUpdateTime() const
{
    const auto hour = static_cast<int>(m_updateTime);
    const auto minute = static_cast<int>((m_updateTime - hour) * 60);
    const auto second = static_cast<int>((m_updateTime - (hour + minute / 60.0)) * 60 * 60);
    return QString::number(hour) + ":" + QString::number(minute) + ":" + QString::number(second);
}

[[maybe_unused]] QString VideoPlatform::GetPlayName() const
{
    return m_playName;
}

[[nodiscard]] int VideoPlatform::GetReadEpisode() const
{
    return m_readEpisode;
}

[[nodiscard]] int VideoPlatform::GetUpdateEpisode() const
{
    return m_updateEpisode;
}

[[nodiscard]] int VideoPlatform::GetAllEpisode() const
{
    return m_allEpisode;
}

[[nodiscard]] QMap<Qt::DayOfWeek, long long> VideoPlatform::GetDayOfWeek()
{
    return m_dayOfWeekToLongMap;
}

[[nodiscard]] QString VideoPlatform::GetNote() const
{
    return m_note;
}

/**
 * SetRecord: 设置各字段的数据
 *
 * @param tupleRecord 传入的tuple数据 转成 类成员变量的数据
 */
void VideoPlatform::SetRecord(const tuple_record &tupleRecord)
{
    auto [id,
        updateTime,
        playName,
        readEpisode,
        updateEpisode,
        allEpisode,
        Mon,
        Tue,
        Wed,
        Thu,
        Fri,
        Sat,
        Sun,
        note
    ] = tupleRecord;

    m_id = id;
    m_updateTime = updateTime;
    m_playName = playName;
    m_readEpisode = readEpisode;
    m_updateEpisode = updateEpisode;
    m_allEpisode = allEpisode;
    m_dayOfWeekToLongMap[Qt::DayOfWeek::Monday] = Mon;
    m_dayOfWeekToLongMap[Qt::DayOfWeek::Tuesday] = Tue;
    m_dayOfWeekToLongMap[Qt::DayOfWeek::Wednesday] = Wed;
    m_dayOfWeekToLongMap[Qt::DayOfWeek::Thursday] = Thu;
    m_dayOfWeekToLongMap[Qt::DayOfWeek::Friday] = Fri;
    m_dayOfWeekToLongMap[Qt::DayOfWeek::Saturday] = Sat;
    m_dayOfWeekToLongMap[Qt::DayOfWeek::Sunday] = Sun;
    m_note = note;
}

void VideoPlatform::UpdateRecord(const QString &tableName)
{

}